/*
* 坡度坡向分析对象
* 需要带法线地形
* 分析区域
* 坡度区间
* 填充模式
* 透明度
* */

class terrainSlopeAnalysis{
  constructor(viewer) {
    this.viewer = viewer
    //坡度坡向对象
    this.slope = new Cesium.SlopeSetting()
    //坡度坡向分析初始化参数
    this.slope.DisplayMode = Cesium.SlopeSettingEnum.DisplayMode.FACE
    this.slope.MaxVisibleValue = document.getElementById("widemaxR").value
    this.slope.MinVisibleValue =  document.getElementById("wideminR").value
    let colorTable = new Cesium.ColorTable()
    colorTable.insert(0, new Cesium.Color(255/255, 0/255, 0/255))
    colorTable.insert(20, new Cesium.Color(221/255, 224/255, 7/255))
    colorTable.insert(30, new Cesium.Color(20/255, 187/255, 18/255))
    colorTable.insert(50, new Cesium.Color(0, 161/255, 1))
    colorTable.insert(80, new Cesium.Color(9/255, 9/255, 255/255))
    this.slope.ColorTable = colorTable
    this.slope.Opacity = document.getElementById("trans").value
    this.SlopePolygon=new Cesium.DrawHandler(viewer,Cesium.DrawMode.Polygon,0)
    this.SlopePolygon.activeEvt.addEventListener((isActive)=>{
      if(isActive == true){
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
        if(document.body.classList.contains('drawCur')){
          document.body.classList.remove('drawCur')
        }
        document.body.classList.add('drawCur')
      }
      else{
        this.viewer.enableCursorStyle = true;
        document.body.classList.remove('drawCur')
      }
    });
    this.SlopePolygon.drawEvt.addEventListener((result)=>{
      if(!result.object.positions){
        this.SlopePolygon.polygon.show = false
        this.SlopePolygon.polyline.show = false
        this.SlopePolygon.deactivate()
        this.SlopePolygon.activate()
        return
      };
      let array = [].concat(result.object.positions)
      let positions = []
      for(let i = 0, len = array.length; i < len; i ++){
        let cartographic = Cesium.Cartographic.fromCartesian(array[i])
        let longitude = Cesium.Math.toDegrees(cartographic.longitude)
        let latitude = Cesium.Math.toDegrees(cartographic.latitude)
        let h=cartographic.height
        if(positions.indexOf(longitude)==-1&&positions.indexOf(latitude)==-1){
          positions.push(longitude)
          positions.push(latitude)
          positions.push(h)
        }
      }
      //坡度坡向分析参数设置
      this.slope.CoverageArea = positions
      this.viewer.scene.globe.SlopeSetting = {
        slopeSetting : this.slope,
        analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
      };
      this.SlopePolygon.polygon.show = false
      this.SlopePolygon.polyline.show = false
      this.SlopePolygon.deactivate()
    });

  }

  remove(){
    //清理坡度坡向分析结果
    this.viewer.scene.globe.SlopeSetting = {
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_NONE
    };
    if(this.SlopePolygon){
      this.SlopePolygon.polygon.show=false
      this.SlopePolygon.polyline.show=false
    }
  }

  analysis(){
    this.SlopePolygon.activate()
  }

  changeMinVisibleValue(value){
    this.slope.MinVisibleValue = value
    this.viewer.scene.globe.SlopeSetting = {
      slopeSetting : this.slope,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    }
  }

  changeMaxVisibleValue(value){
    this.slope.MaxVisibleValue = value
    this.viewer.scene.globe.SlopeSetting = {
      slopeSetting : this.slope,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    }
  }

  changeDisplayMode(value){
    switch (value){
      case 'ShowFillColor':   this.slope.DisplayMode = Cesium.SlopeSettingEnum.DisplayMode.FACE; break;
      case 'ShowSlopeArrow':   this.slope.DisplayMode = Cesium.SlopeSettingEnum.DisplayMode.ARROW; break;
      case 'ShowColorArrow':   this.slope.DisplayMode = Cesium.SlopeSettingEnum.DisplayMode.FACE_AND_ARROW; break;
      default:break;
    }
    this.viewer.scene.globe.SlopeSetting = {
      slopeSetting : this.slope,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    };
  }

  changeOpacity(value){
    slope.Opacity = value
    this.viewer.scene.globe.SlopeSetting = {
      slopeSetting : this.slope,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    };
  }
}

export {terrainSlopeAnalysis}
